<?php

/**
 * 活动 api类
 */
class activityModel
{


    public function __construct(& $base)
    {
        $this->base = $base;
        $this->db = & $this->base->db;
        $this->redis = & $this->base->redis;
    }
    /**
     * 获取活动的链接
     * @return str
     */
    public function getActivityUrl($actId)
    {
        // 活动链接数据
        $actUrlArr = array(
            'gear1506' => 'http://equipmentday2015.hupu.com',
            // 'gearJu' => 'http://jog-test.mobileapi.hupu.com/show/bmiinfo',

        );

        $url = '';
        isset($actUrlArr[$actId]) && $url = $actUrlArr[$actId];

        return $url;
    }
    /**
     * 活动详情
     * @param int $act_id 活动id
     * @return array
     */
    public function getActivityInfo($act_id)
    {
        $act_id = intval($act_id);

        $result = array();
        $sql = "select * from r_activity where act_id = '{$act_id}'";

        $result = $this->db->getRow($sql);

        if(!empty($result))
        {
            $result['status'] = $this->getActStatus($result['start_time'], $result['end_time']);
        }

        return $result;
    }

    public function incrbyActivityUv($aid)
    {
        return $this->db->update('update r_activity set uv=uv+1 where act_id=' . $aid);
    }

    /**
     * 活动列表
     * @param int $act_id 活动id
     * @param string $direc 数据追回方式  next：获取旧数据   prev: 获取新数据
     * @param int $offset 记录数
     * @return array
     */
    public function activityList($act_id = 0, $direc = 'next', $offset = 10 ,$active='')
    {
        $result = array();
        $sql = 'select act_id, title, thumb_img, img, link, start_time, end_time, joins, uv from r_activity where is_show = 1';

        // if($act_id > 0)
        // {
        //     $sql .=  ($direc == 'next' ? " and act_id < '{$act_id}' order by act_id DESC" : " and act_id > '{$act_id}' order by act_id DESC");
        // }else{
        //    $sql.=" order by act_id DESC ";
        // }
        if($active){
            $sql .= " AND end_time > ".time().' order by act_id desc';
        }else{
            //union里边必须加limit否则排序无效
            $sql='('.$sql.' and end_time>'.time().' order by act_id DESC limit 10000) UNION ('.
                $sql.' and end_time<='. time().'  order by act_id DESC limit 10000)';
        }
        $sql .= " limit {$offset}";

        $result = $this->db->getAll($sql);

        if(!empty($result))
        {
            if($act_id > 0 && $direc != 'next')
            {
                $result = rsort($result);
            }

            foreach ($result as & $value)
            {
                $value['status'] = $this->getActStatus($value['start_time'], $value['end_time']);
                $value['countDown'] = $this->getCountDown($value['start_time'], $value['end_time']);
                $value['link'] = $value['link'];

                // 活动参与人数，活动结束后不再显示人数
                if($this->base->time >= $value['end_time'])
                {
                    $value['joins'] = 0;
                }
                else
                {
                    // 参与人数计算
                    $value['joins'] += $value['uv'];
                }
            }
        }

        return $result;
    }
    /**
     * 活动状态
     * @param int $start_time  活动开始时间
     * @param int $end_time 活动结束时间
     * @return int $status   0：未开始， 1：进行中， 2：已结束
     */
    public function getActStatus($start_time = 0, $end_time = 0)
    {
        $status = 1;

        // 当前时间
        $time = $this->base->time;

        if($time < $start_time)
        {
            $status = 0;
        }
        else if($time > $end_time)
        {
            $status = 2;
        }

        return $status;
    }
    /**
     * 活动倒计时
     * @param int $start_time  活动开始时间
     * @param int $end_time 活动结束时间
     * @return int $status   0：未开始， 1：进行中， 2：已结束
     */
    public function getCountDown($start_time = 0, $end_time = 0)
    {
        // 当前时间
        $result = 0;
        $countDown = $time = $this->base->time;

        if($time < $start_time)
        {
            $countDown = $start_time;
        }
        else if($time < $end_time)
        {
            $countDown = $end_time;
        }

        // 转换为日期形式， 不带前导零
        $result = date("Y-n-j G:", $countDown);
        $result .= intval(date('i:', $countDown)).':';
        $result .= intval(date('s:', $countDown));

        return $result;
    }

    /**
     * 计算活动人数加成
     * @param integer $num
     * @param float $odd
     * @return mixed
     */
    public function getJoinsAddition($num, $odd)
    {
        $showNum = $num;

        // 最大加成人数
        $maxNum = 30000;

        if($showNum < 1)
        {
            $showNum = date('H');
        }
        elseif($showNum < 100)
        {
            $odd > 6 && $odd = 6;
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 59.9 * ((1.1-$odd/10) * 2.4)  * 5 * (1.1 - 100 / $maxNum);
        }
        elseif($showNum < 500)
        {
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 29.1 * 5 * (1 - 500 / $maxNum);
        }
        elseif($showNum < 1000)
        {
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 8.1 * 5 * (1 - 1000 / $maxNum);
        }
        elseif($showNum < 2000)
        {
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 7.1 * 5 * (1 - 2000 / $maxNum);
        }
        elseif($showNum < 5000)
        {
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 6.1 * 4 * (1 - 5000 / $maxNum);
        }
        elseif($showNum < 8000)
        {
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 5.1 * 3 * (1 - 8000 / $maxNum);
        }
        elseif($showNum < 13000)
        {
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 4.1 * 2 * (1 - 13000 / $maxNum);
        }
        elseif($showNum < 15000)
        {
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 3.1 * (1 - 15000 / $maxNum);
        }
        elseif($showNum < 20000)
        {
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 1.1;
        }
        elseif($showNum < 25000)
        {
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 0.5;
        }
        else
        {
            $showNum = $num * (1 + 1 - ($showNum / $maxNum)) * $odd * 0.09;
        }

        $showNum < $num && $showNum = $num;
        $showNum = round($showNum);
        $showNum > $maxNum && $showNum = $maxNum . '+';

        $num > $maxNum && $showNum = $num;

        return $showNum;
    }

    /**
     * 获取进行活动的图片和链接地址
     * @return mixed
     */
    public function getBeginAct()
    {
        $time = time();
        $sql = "select img,link from r_activity where end_time > $time order by act_id desc";
        return $this->db->getAll($sql);
    }

}

?>